package main

import (
	"time"

	"gitee.com/yookie316/goutils/dbx"
	"gitee.com/yookie316/goutils/excel"
)

type Users struct {
	Id         int       `gorm:"column:id" json:"id" excel:"id" form:"id" mapstructure:"id,reamain"`
	Name       string    `gorm:"column:name" json:"name" excel:"name" form:"name" mapstructure:"name,reamain"`
	Birth      time.Time `gorm:"column:birth" json:"birth" excel:"birth" form:"birth" mapstructure:"birth,reamain"`
	Password   string    `gorm:"column:password" json:"password" excel:"password" form:"password" mapstructure:"password,reamain"`
	CreateTime time.Time `gorm:"column:create_time" json:"create_time" excel:"create_time" form:"create_time" mapstructure:"create_time,reamain"`
	UpdateTime time.Time `gorm:"column:update_time" json:"update_time" excel:"update_time" form:"update_time" mapstructure:"update_time,reamain"`
}

func (*Users) TableName() string {
	return "users"
}

type Name string

type Staff struct {
	Id   int64 `json:"id,omitempty"`
	Name Name  `json:"name,omitempty"`
}

var excl *excel.Excel

func main() {

	// staff := new(Staff)
	// staff := []Users{
	// 	{Id: 3, Name: "jack", Birth: time.Now().AddDate(-20, 0, 0)},
	// 	{Id: 4, Name: "alice", Birth: time.Now().AddDate(-25, 0, 0)},
	// }
	// staff := map[string]interface{}{
	// 	"id":   1,
	// 	"name": "ome",
	// }

	// bs, _ := json.Marshal(staff)
	// var m []map[string]interface{}
	// err := json.Unmarshal(bs, &m)
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// fmt.Printf("%#v", m)
	// var ps []Users
	// excl = excel.NewExcel()
	// // err := excl.Excel2Structs("data.xlsx", &ps)
	// f, err := os.Open("data.xlsx")
	// if err != nil {
	// 	log.Fatal("open file error: ", err)
	// }
	// ps, err := excl.Excel2Map(f, &Users{})
	// if err != nil {
	// 	log.Fatal("parse excel error: ", err)
	// }
	// err = insertToDb(ps)
	// if err != nil {
	// 	log.Fatal("insert error: ", err)
	// }

	// if err != nil {
	// 	log.Fatal(err)
	// }
	// fmt.Printf("persons : %#v \n", ps)
	// err = insertToDb(ps)
	// fmt.Println("insert error:", err)
	// fmt.Println(time.Now().AddDate(-20, 0, 0).Format("2006-01-02"))
	// data := []Users{
	// 	{Id: 3, Name: "jack", Birth: time.Now().AddDate(-20, 0, 0)},
	// 	{Id: 4, Name: "alice", Birth: time.Now().AddDate(-25, 0, 0)},
	// }
	// err := excl.Save2Excel(data, &Users{}, "abc/data.xlsx")
	// fmt.Printf("%#v", err)
	// fmt.Println(err)

}

func insertToDb(data []map[string]interface{}) error {
	db := dbx.GetDb("root:secret@tcp(192.168.50.20:3306)/cem?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai", true)

	return db.Table("test_users").Omit("id").CreateInBatches(data, 10).Error
}

// func writeToExcel() error {
// 	data := []Users{
// 		Users{Id: 3, Name: "jack", Birth: time.Now().AddDate(20, 0, 0)},
// 		Users{Id: 4, Name: "alice", Birth: time.Now().AddDate(25, 0, 0)},
// 	}

// 	return excl.Data2Excel("data2.xlsx", &data)
// }
